from sklearn.cluster import KMeans
import numpy as np


def  loadData (filePath):
    fr = open(filePath,'r+')
    lines = fr.readlines()
    retDate = []
    retCityName = []
    for line in lines:
        items = line.strip().split(',')
        retCityName.append(items[0])
        retDate.append([float(items[i]) for i in range(1,len(items))])
    for i in range(1,len(items)):
        return retDate,retCityName
    pass


if __name__ == '__main__' :
    data,cityName = loadData('city.txt')
    km = KMeans(n_clusters=3) #聚为几类，欧式距离
    label = km.fit_predict(data)
    expenses = np.sum(km.cluster_centers_,axis=1)
    CityCluster = [[],[],[]]  #按label 分簇
    for i in range(len(cityName)):
        CityCluster[label[i]].append(cityName[i])
    for i in range(len(CityCluster)): #平均划分
        print("Expenses:%.2f"%expenses[i])
        print(CityCluster[i])



